import * as activityAPI from '@/api/activity'

const activity = {
  namespaced: true,
  state: {
    activities: [],
    currentActivity: null,
    loading: false,
    filters: {
      categoryId: '',
      status: '',
      keyword: ''
    },
    pagination: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    myActivities: [],
    participatedActivities: [],
    participatedTotal: 0,
    createdActivities: [],
    createdTotal: 0,
    relatedActivities: [],
    hotActivities: [],
    upcomingActivities: []
  },
  
  mutations: {
    SET_ACTIVITIES(state, activities) {
      state.activities = activities
    },
    SET_CURRENT_ACTIVITY(state, activity) {
      state.currentActivity = activity
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_FILTERS(state, filters) {
      state.filters = filters
    },
    SET_PAGINATION(state, pagination) {
      state.pagination = pagination
    },
    SET_MY_ACTIVITIES(state, activities) {
      state.myActivities = activities
    },
    SET_PARTICIPATED_ACTIVITIES(state, { activities, total }) {
      state.participatedActivities = activities
      state.participatedTotal = total
    },
    SET_CREATED_ACTIVITIES(state, { activities, total }) {
      state.createdActivities = activities
      state.createdTotal = total
    },
    SET_RELATED_ACTIVITIES(state, activities) {
      state.relatedActivities = activities
    },
    SET_HOT_ACTIVITIES(state, activities) {
      state.hotActivities = activities
    },
    SET_UPCOMING_ACTIVITIES(state, activities) {
      state.upcomingActivities = activities
    }
  },
  
  actions: {
    // 获取活动列表
    async getActivityList({ commit, state }, params) {
      commit('SET_LOADING', true)
      try {
        const response = await activityAPI.getActivityList(params)
        commit('SET_ACTIVITIES', response.data || [])
        commit('SET_PAGINATION', {
          ...state.pagination,
          total: response.total || 0
        })
        return response
      } catch (error) {
        console.error('获取活动列表失败:', error)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 获取活动详情
    async getActivityDetail({ commit }, activityId) {
      commit('SET_LOADING', true)
      try {
        const response = await activityAPI.getActivityDetail(activityId)
        commit('SET_CURRENT_ACTIVITY', response)
        return response
      } catch (error) {
        console.error('获取活动详情失败:', error)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 创建活动
    async createActivity(_, activityData) {
      try {
        return await activityAPI.createActivity(activityData)
      } catch (error) {
        console.error('创建活动失败:', error)
        throw error
      }
    },
    
    // 更新活动
    async updateActivity(_, { activityId, activityData }) {
      try {
        return await activityAPI.updateActivity(activityId, activityData)
      } catch (error) {
        console.error('更新活动失败:', error)
        throw error
      }
    },
    
    // 删除活动
    async deleteActivity(_, activityId) {
      try {
        return await activityAPI.deleteActivity(activityId)
      } catch (error) {
        console.error('删除活动失败:', error)
        throw error
      }
    },
    
    // 获取我的活动
    async getMyActivities({ commit }) {
      commit('SET_LOADING', true)
      try {
        const response = await activityAPI.getMyActivities()
        commit('SET_MY_ACTIVITIES', response || [])
        return response
      } catch (error) {
        console.error('获取我的活动失败:', error)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 获取用户参与的活动
    async getUserParticipatedActivities({ commit, dispatch }, params) {
      dispatch('showLoading', null, { root: true })
      try {
        const response = await activityAPI.getUserParticipatedActivities(params)
        commit('SET_PARTICIPATED_ACTIVITIES', {
          activities: response.data || [],
          total: response.total || 0
        })
        return response
      } catch (error) {
        console.error('获取参与活动失败:', error)
        throw error
      } finally {
        dispatch('hideLoading', null, { root: true })
      }
    },
    
    // 获取用户创建的活动
    async getUserCreatedActivities({ commit, dispatch }, params) {
      dispatch('showLoading', null, { root: true })
      try {
        const response = await activityAPI.getUserCreatedActivities(params)
        commit('SET_CREATED_ACTIVITIES', {
          activities: response.data || [],
          total: response.total || 0
        })
        return response
      } catch (error) {
        console.error('获取创建活动失败:', error)
        throw error
      } finally {
        dispatch('hideLoading', null, { root: true })
      }
    },
    
    // 获取热门活动
    async getHotActivities({ commit }) {
      try {
        const response = await activityAPI.getHotActivities()
        commit('SET_HOT_ACTIVITIES', response || [])
        return response
      } catch (error) {
        console.error('获取热门活动失败:', error)
        throw error
      }
    },
    
    // 获取即将开始的活动
    async getUpcomingActivities({ commit }) {
      try {
        const response = await activityAPI.getUpcomingActivities()
        commit('SET_UPCOMING_ACTIVITIES', response || [])
        return response
      } catch (error) {
        console.error('获取即将开始的活动失败:', error)
        throw error
      }
    },
    
    // 获取相关活动
    async getRelatedActivities({ commit }, params) {
      try {
        const response = await activityAPI.getRelatedActivities(params)
        commit('SET_RELATED_ACTIVITIES', response || [])
        return response
      } catch (error) {
        console.error('获取相关活动失败:', error)
        throw error
      }
    },
    
    // 取消活动
    async cancelActivity(_, { activityId, reason }) {
      try {
        return await activityAPI.cancelActivity(activityId, { reason })
      } catch (error) {
        console.error('取消活动失败:', error)
        throw error
      }
    },
    
    // 获取活动列表（支持分类筛选）
    async getActivities({ commit }, params) {
      commit('SET_LOADING', true)
      try {
        const response = await activityAPI.getActivityList(params)
        commit('SET_ACTIVITIES', response.data || [])
        return response
      } catch (error) {
        console.error('获取活动列表失败:', error)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    }
  },
  
  getters: {
    // 获取活动列表
    activities: state => state.activities,
    
    // 获取当前活动
    currentActivity: state => state.currentActivity,
    
    // 获取活动筛选条件
    filters: state => state.filters,
    
    // 获取分页信息
    pagination: state => state.pagination,
    
    // 获取我的活动
    myActivities: state => state.myActivities,
    participatedActivities: state => state.participatedActivities,
    participatedTotal: state => state.participatedTotal,
    createdActivities: state => state.createdActivities,
    createdTotal: state => state.createdTotal,
    
    // 获取相关活动
    relatedActivities: state => state.relatedActivities,
    
    // 获取热门活动
    hotActivities: state => state.hotActivities,
    
    // 获取即将开始的活动
    upcomingActivities: state => state.upcomingActivities,
    
    // 根据ID获取活动
    getActivityById: state => id => {
      return state.activities.find(activity => activity.id === id)
    }
  }
}

export default activity